import socket
import threading


# # 构造客户端套接字实例
# client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#
# # 连接服务器
# SERVER_ADDR = ("192.168.11.151", 60000)
# client_socket.connect(SERVER_ADDR)
#
# # 发送消息
# send_msg = input("输入发送的内容")
# send_bytes = send_msg.encode("gbk")
# client_socket.send(send_bytes)
# print("发送成功")
#
# # 接收消息
# recv_bytes = client_socket.recv(1024)
# recv_msg = recv_bytes.decode("gbk")
# print(f"收到消息{recv_msg}")


# def t_send_msg(client_socket):
#     while True:
#         send_msg = input("输入要发送的信息")
#         send_bytes = send_msg.encode("gbk")
#         client_socket.send(send_bytes)
#         print(f"发送成功{send_msg}")
#
#
# def t_recv_main(client_socket):
#     while True:
#         recv_bytes = client_socket.recv(1024)
#         recv_msg = recv_bytes.decode("gbk")
#         print(f"成功收到{recv_msg}")
#
#
# def main():
#     client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#     SERVER_ADDR = ("192.168.11.151", 60000)
#     client_socket.connect(SERVER_ADDR)
#
#     t_send = threading.Thread(target=t_send_msg, args=(client_socket, ))
#     t_recv = threading.Thread(target=t_recv_main, args=(client_socket, ))
#     t_send.start()
#     t_recv.start()
#
#
# if __name__ == '__main__':
#     main()


# # 构造客服端套接字实例
# client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#
# # 连接客户端
# SERVER_ADDR = ("192.168.11.151", 60000)
# client_socket.connect(SERVER_ADDR)
#
# # 发送信息
# send_msg = input("请输入要发送信息")
# send_bytes = send_msg.encode("gbk")
# client_socket.send(send_bytes)
# print(f"成功发送信息{send_msg}")
#
# # 接收信息
# recv_bytes = client_socket.recv(1024)
# recv_msg = recv_bytes.decode("gbk")
# print(f"收到信息{recv_msg}")


def t_send_main(client_socket):
    # 发送信息
    while True:
        send_msg = input("请输入要发送信息：")
        send_bytes = send_msg.encode("gbk")
        client_socket.send(send_bytes)
        print(f"成功发送信息{send_msg}")


def t_recv_main(client_socket):
    # 接收信息
    while True:
        recv_bytes = client_socket.recv(1024)
        recv_msg = recv_bytes.decode("gbk")
        print(f"成功接收信息{recv_msg}")


def main():
    # 构造客户端的套接字实例
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # 连接客户端
    SERVER_ADDR = ("192.168.11.151", 60000)
    client_socket.connect(SERVER_ADDR)

    # 发送信息
    t_send = threading.Thread(target=t_send_main, args=(client_socket, ))
    t_send.start()

    # 接收信息
    t_recv = threading.Thread(target=t_recv_main, args=(client_socket, ))
    t_recv.start()


if __name__ == '__main__':
    main()
